package com.sky.jtail.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;


@EqualsAndHashCode(callSuper = true)
@Data
public class TailThread extends Thread{


	private WebSocketSession session;

	private InputStream inputStream ;

	private BufferedReader reader;



	public TailThread() {
	}



	public TailThread(BufferedReader reader, WebSocketSession session) {
		this.reader =reader;
		this.session =session;
	}


//	@Override
//	public void run() {
//		String line;
//
//		try {
//			while((line = reader.readLine()) != null) {
//
//				String result = "";
//				// 将实时日志发送给客户端
//				//每150字符换一行
//				int length = line.length();
//				int lineSize = 160;
//				if(length<=lineSize){
//					result = "<span class='log-line'>" + line + "</span><br>";
//				}else {
//					int lineNum = length/lineSize +1;
//					for(int i =0 ;i<lineNum ;i++){
//						int start = i*lineSize;
//						int end = (i+1 == lineNum)? length:(i+1)*lineSize;
//						String logLine = "<span class='log-line'>" + line.substring(start,end) + "</span><br>";
//						result= result + logLine;
//
//					}
//				}
//
//				session.sendMessage(new TextMessage(result));
//
//			}
//		} catch (IOException e) {
//			//e.printStackTrace();
//		}
//	}

	@Override
	public void run() {
		String line;

		try {
			while((line = reader.readLine()) != null) {

				String result = "<span class='log-line'>" + line + "</span><br>";

				session.sendMessage(new TextMessage(result));

			}
		} catch (IOException e) {
			//e.printStackTrace();
		}
	}


}